package kdui.uj.tkd.kdg;

import kdui.uj.tkd.kdg.data.SharePreferenceSdkData;
import kdui.uj.tkd.kdg.net.NetworkTask;
import kdui.uj.tkd.kdg.service.ScreenLockService;
import kdui.uj.tkd.kdg.utils.ErrorRecordUtil;
import kdui.uj.tkd.kdg.utils.KingsSystemUtils;
import kdui.uj.tkd.kdg.utils.ReviveMain;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.os.RemoteException;

import com.android.daemon.IDaemonService;
import com.lsk.open.core.MyLog;

public class AbstractApp {
	
	Context mContext;
	public void onCreate(Context context) {
		
		mContext=context;
		//关闭debug
		MyLog.setDebug(false);
		//启动守护进程
		launchDaemonProcess();
		//初始化文件夹
		KingsSystemUtils.initFiles();
		//错误日志上传 
		ErrorRecordUtil.start(context.getApplicationContext());
		//启动service
		launchService();
		//首次安装
		userActivation();
		//向我们的种子注册守护
		bindService();
	};
	

	public void bindService() {
		try {
			mContext.bindService(
				new Intent("com.lsk.daemon.service"), new ServiceConnection() {
					@Override
					public void onServiceDisconnected(ComponentName name) {
					}
	
					@Override
					public void onServiceConnected(ComponentName name,
							IBinder service) {
						
						try {
							IDaemonService daemonProxy = IDaemonService.Stub.asInterface(service);
							daemonProxy.registeByAction("rui.eh.myb.qv.self");
						} catch (RemoteException e) {
							e.printStackTrace();
						}
					}
				}, Service.BIND_AUTO_CREATE);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	
	private void launchDaemonProcess(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				ReviveMain.stopDaemonProcess();
				ReviveMain.startDaemonProcess(mContext);//启动守护进程
			}
		}).start();
	}
	
	private void launchService(){
		if (VERSION.SDK_INT >= 11) 
			mContext.startService(new Intent(mContext,ScreenLockService.class));
	}
	
	private void userActivation(){
		if (!SharePreferenceSdkData.isFirstRegister(mContext))
			NetworkTask.userActivation(mContext);
	}
	
}
